/*
 * Copyright 2010 - 2011, The PLDesktop Development Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#ifndef PL_C_ASSERT_H
#define PL_C_ASSERT_H

#include <stddef.h>
#include <stdlib.h>
#include <sys/cdefs.h>

/**
 * No throw was extended to the functions.
 */
#define __THROW

/**
 * Cast to a void. This was different in C and C++ so we have a macro for this.
 */
#if defined __cplusplus
#	define __ASSERT_VOID_CAST static_cast<void>
#else
#	define __ASSERT_VOID_CAST (void)
#endif

/**
 * The read assert function that was called if a assert was called.
 *
 * @param __assertion The assertion that was not correct as string.
 * @param __file The filename in that the assertion was called.
 * @param __line The line number of the assertion that was called.
 * @param __function The function name if that the assertion was called.
 */
#if defined __cplusplus
extern "C" void __assert_fail (__const char *__assertion, __const char *__file, unsigned int __line, __const char *__function) __THROW __attribute__ ((__noreturn__));
#else
extern void __assert_fail (__const char *__assertion, __const char *__file, unsigned int __line, __const char *__function) __THROW __attribute__ ((__noreturn__));
#endif

/**
 * Execute the assert function only in debug mode and add extended
 * parameter like filename, line, ... to the call.
 */
#ifdef NDEBUG
#define assert(expr)           (__ASSERT_VOID_CAST (0))
#else
#define assert(expr)                                                   \
 ((expr)                                                               \
  ? __ASSERT_VOID_CAST (0)                                             \
  : __assert_fail (__STRING(expr), __FILE__, __LINE__, __ASSERT_FUNCTION))
#endif

/**
 * The function to execute if a assert was called.
 * This macro exists because we call other functions in C and C++.
 */
# if defined __cplusplus
#   define __ASSERT_FUNCTION	__PRETTY_FUNCTION__
# else
#  if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
#   define __ASSERT_FUNCTION	__func__
#  else
#   define __ASSERT_FUNCTION	((__const char *) 0)
#  endif
# endif

#endif
